from flask import Blueprint, request
from conn_mysql import ConnectMysql
from .access_token_notify import access_token_verify_func

app_order = Blueprint('app_order', 'api')

@app_order.route('/app_order', methods=['POST'])
def order():
    access_token = request.form.get('access_token')#登录令牌
    user_id = request.form.get('user_id')#用户ID
    direction = request.form.get('direction')#行驶方向
    carpooling_Hitl = request.form.get('carpooling_Hitl')#包车还是拼车
    print([access_token, user_id, direction, carpooling_Hitl])
    if not all([access_token, user_id, direction, carpooling_Hitl]):

        return {
            "status": -1,
            "errorCode": 61,
            "errorMsg": "有参数为空",
            "resultBody": None
        }

    sql = "SELECT * FROM call_car WHERE carpooling_Hitl=%s AND direction=%s ORDER BY go_time DESC"
    sql_ = "SELECT * FROM order_status WHERE order_id=%s"

    with ConnectMysql() as c:

        result = c.select_all(sql, (carpooling_Hitl, direction))

        for i in result:

            i['go_time'] =  i['go_time'].__str__()
            i['regedate'] =  i['regedate'].__str__()

            # 循环sql语句对于项目性能的影响是极其不利的
            order_status_result = c.select_one(sql_,(i['id'],))
            if not order_status_result:
                i['order_status'] = '未处理'
            
            elif order_status_result['order_status'] == 1:

                i['order_status'] = '已处理'

            elif order_status_result['order_status'] == 0:
    
                i['order_status'] = '已取消'

            elif order_status_result['order_status'] == 2:
        
                i['order_status'] = '用户取消'
            elif order_status_result['order_status'] == 3:
            
                i['order_status'] = '用户操作'

    return {
            "status": 1,
            "errorCode": None,
            "errorMsg" : None,
            "resultBody": result

        }

    
